package com.hookgetaway.utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/* compiled from: SettingCompatUtil.java */
public class RomInfoUtil {
    public static String currentRom;
    public static String romVersion;

    public static boolean isCurrentRom(String romName) {
        if (currentRom != null) {
            return currentRom.equals(romName);
        }
        String version = getSystemProperty("ro.miui.ui.version.name");
        romVersion = version;
        if (!TextUtils.isEmpty(version)) {
            currentRom = "MIUI";
        } else {
            version = getSystemProperty("ro.build.version.emui");
            romVersion = version;
            if (!TextUtils.isEmpty(version)) {
                currentRom = "EMUI";
            } else {
                version = getSystemProperty("ro.build.version.opporom");
                romVersion = version;
                if (!TextUtils.isEmpty(version)) {
                    currentRom = "OPPO";
                } else {
                    version = getSystemProperty("ro.vivo.os.version");
                    romVersion = version;
                    if (!TextUtils.isEmpty(version)) {
                        currentRom = "VIVO";
                    } else {
                        String display = Build.DISPLAY;
                        romVersion = display;
                        if (display.toUpperCase().contains("FLYME")) {
                            currentRom = "FLYME";
                        } else {
                            romVersion = "unknown";
                            currentRom = Build.MANUFACTURER.toUpperCase();
                        }
                    }
                }
            }
        }
        return currentRom.equals(romName);
    }

    public static boolean hasIntentHandler(Context context, Intent intent) {
        return queryIntentActivities(context, intent, 65536).size() > 0;
    }

    public static String getCurrentRom() {
        if (currentRom == null) {
            isCurrentRom("");
        }
        return currentRom;
    }

    public static String getSystemProperty(String propertyName) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop " + propertyName).getInputStream()), 1024);
            String propertyValue = reader.readLine();
            reader.close();
            return propertyValue;
        } catch (IOException e) {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                }
            }
            return null;
        }
    }

    public static String getRomVersion() {
        if (romVersion == null) {
            isCurrentRom("");
        }
        return romVersion;
    }

    public static boolean isEmui() {
        return isCurrentRom("EMUI");
    }

    public static boolean isFlyme() {
        return isCurrentRom("FLYME");
    }

    public static boolean isGiONEE() {
        return "GiONEE".equalsIgnoreCase(Build.BRAND);
    }

    public static boolean isMiui() {
        return isCurrentRom("MIUI");
    }

    public static boolean isOneplus() {
        return "oneplus".equalsIgnoreCase(Build.BRAND);
    }

    public static boolean isOppo() {
        return isCurrentRom("OPPO");
    }

    public static boolean isVivo() {
        return isCurrentRom("VIVO");
    }

    public static Intent getApplicationDetailsSettingsIntent(Context context) {
        Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        return intent;
    }

    public static Intent getPermissionSettingsIntent(Context context, String appName) {
        if (isMiui()) {
            return getMiuiPermissionSettingsIntent(context);
        }
        if (isGiONEE()) {
            return getGiONEEPermissionSettingsIntent(context, appName);
        }
        return getApplicationDetailsSettingsIntent(context);
    }

    public static Intent getGiONEEPermissionSettingsIntent(Context context, String appName) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.permission.PermissionAppDetail"));
        intent.putExtra("packagename", context.getPackageName());
        intent.putExtra("title", appName);
        return hasIntentHandler(context, intent) ? intent : getApplicationDetailsSettingsIntent(context);
    }

    public static Intent getMiuiPermissionSettingsIntent(Context context) {
        Intent intent = new Intent();
        intent.setAction("miui.intent.action.APP_PERM_EDITOR");
        intent.putExtra("extra_pkgname", context.getPackageName());
        // 补充设置ComponentName的逻辑
        intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity"));
        if (hasIntentHandler(context, intent)) {
            return intent;
        }
        // 如果上面的ComponentName不工作，尝试另一个
        intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity"));
        if (hasIntentHandler(context, intent)) {
            return intent;
        }
        // 如果都不工作，则返回默认的应用详情设置Intent
        return getApplicationDetailsSettingsIntent(context);
    }

    public static List<ResolveInfo> queryIntentActivities(@NonNull Context context, @NonNull Intent intent, int flags) {
        PackageManager packageManager = context.getPackageManager();
        return packageManager.queryIntentActivities(intent, flags);
    }


}